package com.handle.controller;

import cn.hutool.json.JSONObject;
import com.handle.entity.StockCirculation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author: create by God of SJF
 * @version: v1.0
 * @description: com.handle.controller
 * @date:2021/8/26
 */
@Controller
public class InfoController {

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    KafkaTemplate<String,String> kafka;

    @GetMapping(value = "/info")
    public void info(){
        kafka.send("libin","卧槽99999999");
    }



    @GetMapping(value = "/mongo")
    public void mongo()throws Exception{
        File file = new File("/Users/libin/Desktop/码流转2/2.json");
        Scanner scanner = new Scanner(file);
        while (scanner.hasNext()) {
            String line = scanner.nextLine();
            JSONObject json = new JSONObject(line);
            JSONObject jsonObject = new JSONObject();

            for(String string :map.keySet()){
                jsonObject.putOpt(map.get(string),json.get(string));
            }
            StockCirculation stockCirculation = jsonObject.toBean(StockCirculation.class);
            mongoTemplate.insert(stockCirculation);
        }
    }
    static Map<String, String> map = new HashMap<String, String>() {
        {
           put("_barcode_content", "barcodeContent");
           put("_status", "status");
           put("_operate_time", "operateTime");
           put("_operate_name", "operateName");
           put("_organization_type", "organizationType");
           put("_organization_code", "organizationCode");
           put("_organization_name", "organizationName");
           put("_product_code", "productCode");
           put("_dealers_type", "dealersType");
           put("_dealers_code", "dealersCode");
           put("_dealers_name", "dealersName");
           put("_product_name", "productName");
           put("_batch_code", "batchCode");
           put("_product_line_code", "productLineCode");
           put("_product_line_name", "productLineName");
           put("_workshop_code", "workshopCode");
           put("_workshop_name", "workshopName");
           put("_product_team_code", "productTeamCode");
           put("_product_team", "productTeam");
           put("_warehouse_code", "warehouseCode");
           put("_warehouse_name", "warehouseName");
           put("_towarehouse_code", "toWarehouseCode");
           put("_towarehouse_name", "toWarehouseName");
           put("_workid", "workId");
           put("_batch_no", "batchNo");
        }
    };
}
